package net.frankish.tools;
/*
 * $Header: /u/home/public/Caddy/CVS_ROOT/CaddyWeb_Util/src/ca/caddy/caddyweb/util/XMLSerialization.java,v 1.10 2005/03/21 21:21:29 pascall Exp $
 */
import java.beans.ExceptionListener;
import java.beans.Expression;
import java.beans.PersistenceDelegate;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

/**
 * @version $Revision: 1.10 $
 */
public class XMLSerialization
{
	public static Collection object2XML(Collection col)
	{
		ArrayList res = new ArrayList(col.size());
		for (Iterator i = col.iterator(); i.hasNext(); )
		{
			Object o = i.next();
			String s = object2XML(o);
			res.add(s);
		}
		return res;
	}

	public static Collection xml2Object(Collection col)
	{
		ArrayList res = new ArrayList(col.size());
		for (Iterator i = col.iterator(); i.hasNext(); )
		{
			String s = (String) i.next();
			Object o = xml2Object(s);
			res.add(o);
		}
		return res;
	}

	public static String object2XML(Object o)
	{
		return object2XML(o, null, null);
	}

	public static String object2XML(Object o, Map persistenceMap,
		ExceptionListener exList)
	{
		java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream(1024);
		XMLEncoder enc = new XMLEncoder(out);
		if (exList!=null)
		{
			enc.setExceptionListener(exList);

		}
		if (persistenceMap != null)
		{
			for (Iterator i=persistenceMap.entrySet().iterator(); i.hasNext(); )
			{
				Map.Entry entry = (Map.Entry) i.next();
				enc.setPersistenceDelegate((Class)entry.getKey(), (PersistenceDelegate)entry.getValue());
			}
		}
		
//		Automatic Support for java.sql.Date and java.sql.Time
		enc.setPersistenceDelegate(java.sql.Date.class, new PersistenceDelegate() {
			 
		    protected Expression instantiate(Object oldInstance, java.beans.Encoder out) {
		        java.sql.Date d = (java.sql.Date)oldInstance;
		        return new Expression(oldInstance,
		            d.getClass(),
		            "valueOf",
		            new Object[]{
		                d.toString()
		            }
		        );
		    }
		});
		enc.setPersistenceDelegate(java.sql.Time.class, new PersistenceDelegate() {
			 
		    protected Expression instantiate(Object oldInstance, java.beans.Encoder out) {
		        java.sql.Time d = (java.sql.Time)oldInstance;
		        return new Expression(oldInstance,
		            d.getClass(),
		            "valueOf",
		            new Object[]{
		                d.toString()
		            }
		        );
		    }
		});
		
		enc.writeObject(o);
		enc.close();
		try {
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String res = out.toString();
		return res;
	}

	public static Object xml2Object(String xml)
	{
		Object res= null;
		java.io.ByteArrayInputStream in = new java.io.ByteArrayInputStream(xml.getBytes());
		try{
			XMLDecoder dec = new XMLDecoder(in);
			
		
			dec.setExceptionListener(
				new ExceptionListener()
				{
					public void exceptionThrown(Exception e)
					{
						e.printStackTrace();
					}
				}
			);
			
			res = dec.readObject();
			dec.close();
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return res;
	}

	public static Object[] xmlArgs2Object(Object[] args)
	{
		if (args == null)
		{
			return null;
		}

		Object[] res = new Object[args.length];
		for (int i=0; i<args.length; i++)
		{
			if (args[i] instanceof Collection)
			{
				res[i] = XMLSerialization.xml2Object((Collection)args[i]);
			}
			else
			{
				res[i] = XMLSerialization.xml2Object((String)args[i]);
			}
		}
		return res;
	}

	public static Object[] objectArgs2XML(Object[] args)
	{
		if (args == null)
		{
			return null;
		}

		Object[] res = new Object[args.length];
		for (int i=0; i<args.length; i++)
		{
			if (args[i] instanceof Collection)
			{
				res[i] = XMLSerialization.object2XML((Collection)args[i]);
			}
			else
			{
				res[i] = XMLSerialization.object2XML(args[i]);
			}
		}
		return res;
	}
}
